package com.woniuxy.vuecli.configuration;

import com.woniuxy.vuecli.interceptor.AuthorizationInterceptor;
import com.woniuxy.vuecli.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author author
 * @Create 2024-01-15 10:44
 */
//配置类<bean id="" class="" />
@Configuration
public class MyCorsConfiguration {

    @Autowired
    LoginInterceptor loginInterceptor;

    @Autowired
    AuthorizationInterceptor authorizationInterceptor;

    //自动创建的对象，定义成方法
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            //跨域配置
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")    //允许后端哪些请求可以跨域访问
                        .allowedOrigins("*")  //允许前端端哪些请求可以跨域访问
                        .allowedHeaders("*")  //前端访问允许携带请求头
                        .allowedMethods("*"); //前端访问允许使用哪些请求格式get/post/put/delete
            }

            //拦截器配置


            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(loginInterceptor)
                        .addPathPatterns("/**")                     //设置拦截路径
                        .excludePathPatterns("/employee/login")    //特殊路径放行
                        .excludePathPatterns("/verifycode/create")
                        .excludePathPatterns("/email/send/**")
                        .excludePathPatterns("/menu/**");

                registry.addInterceptor(authorizationInterceptor)
                        .addPathPatterns("/**")                     //设置拦截路径
                        .excludePathPatterns("/employee/login")    //特殊路径放行
                        .excludePathPatterns("/verifycode/create")
                        .excludePathPatterns("/email/send/**")
                        .excludePathPatterns("/menu/**");

            }
        };
    }
}
